package com.jwell.panprovider.ui.activity

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.jwell.cargoapp.ui.base.MVPBaseActivity
import com.jwell.panprovider.R
import com.jwell.panprovider.application.App
import com.jwell.panprovider.application.ConstValue
import com.jwell.panprovider.contract.WaybillListContract
import com.jwell.panprovider.entity.WaybillListBean
import com.jwell.panprovider.model.WaybillListModel
import com.jwell.panprovider.presenter.WaybillListPresenter
import com.jwell.panprovider.tools.DataModel
import com.jwell.panprovider.ui.adapter.WaybillListAdapter
import com.jwell.panprovider.ui.custom.DateChooseWheelViewDialog
import kotlinx.android.synthetic.main.activity_waybill_list_search.*
import kotlinx.android.synthetic.main.empty_layout.*
import kotlinx.android.synthetic.main.error_layout.*
import kotlinx.android.synthetic.main.list_layout.*
import kotlinx.android.synthetic.main.toolbar.*

/**
 * 运单列表搜索界面
 */
class WaybillListSearchActivity : MVPBaseActivity<WaybillListPresenter>(), WaybillListContract.View, View.OnClickListener {
    private var billStateList = mutableListOf<Int>()
    private var haveSearch = false
    private var searchData = WaybillListModel.RequestData()

    private var listData = ArrayList<WaybillListBean>()
    private var adapter = WaybillListAdapter(listData)
    override fun createPresenter(): WaybillListPresenter? {
        return WaybillListPresenter()

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
        setContentView(R.layout.activity_waybill_list_search)
        initToolBar("运单搜索", true, null)
        toolbar_right_text_save.text = "搜索"
        toolbar_right_text_save.visibility = View.VISIBLE
        setListener()
        initData()
    }

    private fun initData() {
        initEmptyAndErrorView()
        loadingLayout.visibility = View.GONE
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        freshLayout.setEnableLoadmoreWhenContentNotFull(true)
        freshLayout.setEnableFooterFollowWhenLoadFinished(true)
        freshLayout.setOnRefreshListener {
            presenter?.fresh()
        }
        freshLayout.setOnLoadmoreListener {
            presenter?.load()
        }
        DataModel.getInstance().waybillSearchData?.let {
            it.billStateList?.let {
                if (it.contains(1)) {
                    setLabel(!rl_item11.isSelected, rl_item11, image_tick_11, text_label11)
                }
                if (it.contains(2)) {
                    setLabel(!rl_item12.isSelected, rl_item12, image_tick_12, text_label12)
                }
                if (it.contains(3)) {
                    setLabel(!rl_item13.isSelected, rl_item13, image_tick_13, text_label13)
                }
                if (it.contains(4)) {
                    setLabel(!rl_item14.isSelected, rl_item14, image_tick_14, text_label14)
                }
                if (it.contains(5)) {
                    setLabel(!rl_item15.isSelected, rl_item15, image_tick_15, text_label15)
                }
                if (it.contains(9)) {
                    setLabel(!rl_item16.isSelected, rl_item16, image_tick_16, text_label16)
                }
            }
            edit_planeCode.setText(it.planCode)
            edit_waybillCode.setText(it.forBillCode)
            edit_receiptCom.setText(it.receivingUnit)
            edit_zhixingdanhanghao.setText(it.mtrlOrderNo)
            edit_goodsName.setText(it.materialName)
            edit_driverName.setText(it.driverName)
            edit_carNum.setText(it.carNum)
        }
    }

    private fun initEmptyAndErrorView() {
        image_empty.setImageResource(R.mipmap.icon_waybill)
        text_empty_describe.text = "无搜索到符合条件的运单"
        ll_error.setOnClickListener {
            errorLayout.visibility = View.GONE
            loadingLayout.visibility = View.VISIBLE
            presenter?.fresh()
        }
    }

    private fun setListener() {
        rl_item11.setOnClickListener(this)
        rl_item12.setOnClickListener(this)
        rl_item13.setOnClickListener(this)
        rl_item14.setOnClickListener(this)
        rl_item15.setOnClickListener(this)
        rl_item16.setOnClickListener(this)
        text_time1.setOnClickListener(this)
        text_time2.setOnClickListener(this)
        text_time3.setOnClickListener(this)
        text_time4.setOnClickListener(this)
        text_time5.setOnClickListener(this)
        text_time6.setOnClickListener(this)
        toolbar_right_text_save.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.rl_item11 -> setLabel(!rl_item11.isSelected, rl_item11, image_tick_11, text_label11)
            R.id.rl_item12 -> setLabel(!rl_item12.isSelected, rl_item12, image_tick_12, text_label12)
            R.id.rl_item13 -> setLabel(!rl_item13.isSelected, rl_item13, image_tick_13, text_label13)
            R.id.rl_item14 -> setLabel(!rl_item14.isSelected, rl_item14, image_tick_14, text_label14)
            R.id.rl_item15 -> setLabel(!rl_item15.isSelected, rl_item15, image_tick_15, text_label15)
            R.id.rl_item16 -> setLabel(!rl_item16.isSelected, rl_item16, image_tick_16, text_label16)
            R.id.text_time1 -> {
                val startDateChooseDialog = DateChooseWheelViewDialog(this@WaybillListSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time1.text = time
                })
                startDateChooseDialog.setDateDialogTitle("创建开始时间")
                startDateChooseDialog.setTimePickerGone(true)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.text_time2 -> {
                val startDateChooseDialog = DateChooseWheelViewDialog(this@WaybillListSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time2.text = time
                })
                startDateChooseDialog.setDateDialogTitle("创建结束时间")
                startDateChooseDialog.setTimePickerGone(true)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.text_time3 -> {
                val startDateChooseDialog = DateChooseWheelViewDialog(this@WaybillListSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time3.text = time
                })
                startDateChooseDialog.setDateDialogTitle("供应开始时间")
                startDateChooseDialog.setTimePickerGone(true)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.text_time4 -> {
                val startDateChooseDialog = DateChooseWheelViewDialog(this@WaybillListSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time4.text = time
                })
                startDateChooseDialog.setDateDialogTitle("供应结束时间")
                startDateChooseDialog.setTimePickerGone(true)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.text_time5 -> {
                val startDateChooseDialog = DateChooseWheelViewDialog(this@WaybillListSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time5.text = time
                })
                startDateChooseDialog.setDateDialogTitle("收料开始时间")
                startDateChooseDialog.setTimePickerGone(false)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.text_time6 -> {
                val startDateChooseDialog = DateChooseWheelViewDialog(this@WaybillListSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time6.text = time
                })
                startDateChooseDialog.setDateDialogTitle("收料结束时间")
                startDateChooseDialog.setTimePickerGone(false)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.toolbar_right_text_save -> {
                when (toolbar_right_text_save.text) {
                    "重新搜索" -> {
                        list_layout.visibility = View.GONE
                        scrollView_search.visibility = View.VISIBLE
                        haveSearch = false
                    }
                    "搜索" -> {
                        searchData.billStateList = billStateList
                        searchData.planCode = edit_planeCode.text.toString()
                        searchData.forBillCode = edit_waybillCode.text.toString()
                        searchData.receivingUnit = edit_receiptCom.text.toString()
                        searchData.mtrlOrderNo = edit_zhixingdanhanghao.text.toString()
                        searchData.materialName = edit_goodsName.text.toString()
                        searchData.driverName = edit_driverName.text.toString()
                        searchData.carNum = edit_carNum.text.toString()
                        searchData.stateDate = text_time1.text.toString()
                        searchData.endDate = text_time2.text.toString()
                        searchData.planStartDate = text_time3.text.toString()
                        searchData.planEndDate = text_time4.text.toString()
                        searchData.feedingStartTime = text_time5.text.toString()
                        searchData.feedingEndTime = text_time6.text.toString()
                        searchData.skipCount = 0
                        showProgress("", true)
                        DataModel.getInstance().saveWaybillSearchData(searchData)
                        presenter?.searchListData(searchData)
                        haveSearch = true
                        list_layout.visibility = View.VISIBLE
                        scrollView_search.visibility = View.GONE
                    }
                }
                when (haveSearch) {
                    true -> {
                        toolbar_right_text_save.text = "重新搜索"
                    }
                    false -> {
                        toolbar_right_text_save.text = "搜索"
                        initToolBar("运单搜索", true, null)
                    }
                }
            }
        }
    }

    private fun setLabel(c1: Boolean, rl_item: RelativeLayout, imageView: ImageView, textView: TextView) {
        rl_item.isSelected = c1
        setTextColor(c1, textView, imageView)
        when (rl_item) {
            rl_item11 -> {
                if (c1) {
                    billStateList.add(1)
                } else {
                    billStateList.remove(1)
                }
            }
            rl_item12 -> {
                if (c1) {
                    billStateList.add(2)
                } else {
                    billStateList.remove(2)
                }
            }
            rl_item13 -> {
                if (c1) {
                    billStateList.add(3)
                } else {
                    billStateList.remove(3)
                }
            }
            rl_item14 -> {
                if (c1) {
                    billStateList.add(4)
                } else {
                    billStateList.remove(4)
                }
            }
            rl_item15 -> {
                if (c1) {
                    billStateList.add(5)
                } else {
                    billStateList.remove(5)
                }
            }
            rl_item16 -> {
                if (c1) {
                    billStateList.add(9)
                } else {
                    billStateList.remove(9)
                }
            }
        }
    }

    private fun setTextColor(isChecked: Boolean, textView: TextView, imageView: ImageView) {
        when (isChecked) {
            true -> {
                textView.setTextColor(application.resources.getColor(R.color.black))
                imageView.visibility = View.VISIBLE
            }
            false -> {
                textView.setTextColor(application.resources.getColor(R.color.c999999))
                imageView.visibility = View.GONE
            }
        }
    }

    override fun showListData(data: List<WaybillListBean>, isFresh: Boolean) {
        loadingLayout.visibility = View.GONE
        errorLayout.visibility = View.GONE
        emptyLayout.visibility = View.GONE
        if (isFresh) {
            listData.clear()
            freshLayout.finishRefresh()
            //设置可以加载更多
            freshLayout.resetNoMoreData()
            if (App.DEBUG) println("设置可以加载更多")
        } else {
            freshLayout.finishLoadmore()
        }
        listData.addAll(data)
        adapter.notifyDataSetChanged()
        if (listData.size == 0) {
            emptyLayout.visibility = View.VISIBLE
        } else if (data.size < ConstValue.MAXVUALUE) {
            freshLayout.finishLoadmoreWithNoMoreData()
            if (App.DEBUG) println("设置不可以加载更多")
        }
    }

    override fun showListDataFailed(isFresh: Boolean, str: String) {
        loadingLayout.visibility = View.GONE
        errorLayout.visibility = View.GONE
        emptyLayout.visibility = View.GONE
        if (isFresh) {
            freshLayout.finishRefresh(false)
            if (listData.size == 0) errorLayout.visibility = View.VISIBLE
            freshLayout.finishLoadmore(false)
        } else {
            freshLayout.finishLoadmore(false)
        }
    }

    override fun setSearchTotalCount(count: Int) {
        if (count > 0) {
            initToolBar("搜索到" + count + "条运单", true, null)
        } else {
            initToolBar("未搜索到相关运单", true, null)
        }
    }

}
